package com.lebang.controller.sys;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lebang.aop.ConfigRecord;
import com.lebang.common.Login;
import com.lebang.domain.AppAdContro;
import com.lebang.domain.AppRousePlan;
import com.lebang.domain.TaskRouse;
import com.lebang.example.AppAdControExample;
import com.lebang.example.AppRousePlanExample;
import com.lebang.model.Whether;
import com.lebang.service.AppAdControService;
import com.lebang.service.DownAppService;
import com.lebang.service.NewRouseService;
import com.lebang.service.earn.AppRousePlanService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;


/**
 * 数据源排序
 *
 * @author hwq
 * @date 2020-03-16
 */
@Controller
@RequestMapping("/sys/appadcontro")
public class AppAdControController {
    @Autowired
    private AppAdControService baseService;
    @Autowired
    private NewRouseService newRouseService;
    @Autowired
    private DownAppService downAppService;
    @Autowired
    private AppRousePlanService appRousePlanService;

    @GetMapping("/list")
    public String list() {
        return "/appadcontro/list";
    }

    @GetMapping("/add")
    public String add(Integer id, Model model,HttpSession session) {
        AppAdContro vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new AppAdContro();
            vo.setSdkFirstKey("CSJ");
            vo.setSdkLastKey("CSJ");
        }
        List<TaskRouse> rouses = newRouseService.selectAll();
        rouses = newRouseService.checkRoot(rouses,session);
        List<AppRousePlan> list=appRousePlanService.selectByExample(new AppRousePlanExample());
        model.addAttribute("list", rouses);
        model.addAttribute("item", vo);
        model.addAttribute("app", list);
        return "/appadcontro/edit";
    }


    @PostMapping("/edit")
    @ResponseBody
    @ConfigRecord
    public JsonData edit(AppAdContro vo) {
        if (vo.getId() != null) {
            vo.setModifyTime(LocalDateTime.now());
            baseService.updateById(vo);
        } else {
            vo.setCreateTime(LocalDateTime.now());
            vo.setHasDeleted(0);
            vo.setEnable(1);
            baseService.insertModel(vo);
        }
        baseService.initByKey(vo);
        return Callback.success("success", "修改成功");
    }

    @Login
    @PostMapping("/delete")
    @ResponseBody
    public JsonData delete(Integer id) {
        AppAdContro vo =baseService.selectById(id);
        baseService.deleteById(id);
        vo.setEnable(Whether.NO.getDbIndex());
        baseService.initByKey(vo);
        return Callback.success("success", "删除成功");
    }

    @Login
    @PostMapping("/enable")
    @ResponseBody
    @ConfigRecord
    public JsonData enable(@RequestBody AppAdContro appAdContro) {
        AppAdContro vo = baseService.selectById(appAdContro.getId());
        vo.setEnable(appAdContro.getEnable());
        vo.setModifyTime(LocalDateTime.now());
        baseService.updateById(vo);
        baseService.initByKey(vo);
        return Callback.success("success", "操作成功");
    }

    @Login
    @PostMapping("/page")
    @ResponseBody
    public JSONObject page(Integer id, Long page, Integer limit, AppAdControExample example,String adName) {
        JSONObject jsonObject = new JSONObject();
        if(!TextUtil.isEmpty(adName)){
            example.createCriteria().andAdNameLike(adName+"%");
        }
        example.setOffset((page - 1) * limit);
        example.setLimit(limit);
        List<AppAdContro> data = baseService.selectByExample(example);
        data.forEach(s->{
            s.setTodayCount(baseService.getCountByKey(s.getAdKey()));
            s.setYesterDayCount(baseService.getCountByKeyYesterday(s.getAdKey()));
            s.setMemberCount((int) baseService.getMemberCountByKeyToDay(s.getAdKey()));
        });
        jsonObject.put("data", data);
        jsonObject.put("count", baseService.countByExample(example));
        jsonObject.put("code", 0);
        return jsonObject;
    }
    @GetMapping("/data")
    public String data1() {
        return "/appadcontro/data";
    }
    @Login
    @PostMapping("/data")
    @ResponseBody
    public JSONObject data(HttpSession session) {
        JSONObject object=new JSONObject();
        JSONArray jsonObject = new JSONArray();
        List<TaskRouse> rouses = newRouseService.selectAll();
        rouses = newRouseService.checkRoot(rouses,session);
        LocalDate now =LocalDate.now();
        LocalDateTime star=LocalDateTime.of(now, LocalTime.MIN);
        LocalDateTime end=LocalDateTime.of(now, LocalTime.MAX);
        LocalDateTime yesStar=LocalDateTime.of(now.minusDays(1), LocalTime.MIN);
        LocalDateTime yesEnd=LocalDateTime.of(now.minusDays(1), LocalTime.MAX);
        rouses.forEach(s->{
            JSONObject object1=new JSONObject();
            object1.put("task_name",s.getTaskName());
            object1.put("today",baseService.getCountBySdkKey(s.getSdkKey()));
            object1.put("yeterday",baseService.getCountBySdkKeyYesterday(s.getSdkKey()));
            object1.put("success_today",downAppService.selectSuccessByTime(star,end,s.getSdkKey(),1));
            object1.put("success_yeterday",downAppService.selectSuccessByTime(yesStar,yesEnd,s.getSdkKey(),1));
            jsonObject.add(object1);
        });
        object.put("data", jsonObject);
        object.put("count", jsonObject.size());
        object.put("code", 0);
        return object;
    }


}
